package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //添加会员
    @Override
    public void add(Member member) {
        //通过MD5算法将密码进行加密
        String password = member.getPassword();
        if (password!=null){
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.add(member);
    }

    //删除会员
    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    //编辑会员信息
    @Override
    public void edit(Member member) {
        //将密码进行MD5加密保存
        String password = member.getPassword();
        if (password!=null){
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.edit(member);
    }

    //根据ID查询会员信息
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //根据手机号查询会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //根据月份查找会员人数
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            //查询日期格式为：2020.12.31
            String date = month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    //分页查询会员信息
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //使用mybatis框架提供的PageHelper进行分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);

        //将Page对象转化为PageResult对象
        PageResult result = new PageResult(page.getTotal(), page.getResult());

        return result;
    }

    @Override
    public Integer add4Order(Member member) {
        String telephone = member.getPhoneNumber();
        String name = member.getName();
        Member member4Order = memberDao.findByTelephone(telephone);
        if (member4Order==null){
            //判断用户是否存在，如果为空则创建
//            member = new Member();
            //不是会员，自动注册为会员（自动将当前用户保存到会员表）
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            member.setName(name);
            Integer id = memberDao.add4Order(member);
            Integer member_id = member.getId();
//            System.out.println(id);
            return member_id;
        }else {
            Integer member_id = member4Order.getId();
            return member_id;
        }
    }

    @Override
    public List<Member> finAll() {
        return memberDao.findAll();
    }
}
